<template>
  <basic-container>
    <avue-crud
        :defaults.sync="fromField"
        :data="data"
        :option="option"
        ref="curd"
        v-model="form"
        :permission="permissionList"
        :table-loading="loading"
        :before-open="beforeOpen"
        @row-save="save"
        @row-del="del"
        @row-update="update"
        @current-change="currentChange"
        @size-change="sizeChange"
        @search-change="search"
        @refresh-change="refresh"
        @selection-change="selectChange"
        @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button
            type="primary"
            size="small"
            icon="el-icon-download"
            v-if="permission.menu_excel"
            @click="exports"
        >导 出
        </el-button>
        <el-button

            type="primary"
            size="small"
            icon="el-icon-setting"
            @click="createBtn"
            v-loading.fullscreen.lock="fullscreenLoading"
        >生成按钮
        </el-button>
        <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            v-if="permission.menu_delete"
            @click="deletaAll"
        >删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>
<script>
import {mapGetters} from "vuex";
import {createBtn, del, exports, menuTreeList, query, save, update} from "@/api/system/menu";
import iconList from "@/config/iconList";


export default {
  data() {
    return {
      fromField: {},
      fullscreenLoading: false,
      loading: true,
      form: {},
      data: [],
      selectionList: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        menuWidth: 300,
        height: 'auto',
        calcHeight: 100,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        selection: true,
        dialogClickModal: false,
        index: true,
        enter: true,
        copyBtn: true,
        viewBtn: true,
        columnBtn:false,
        tip:false,
        column: [
          {
            label: "菜单名称",
            prop: "name",
            search: true,
            align: "left",
            rules: [
              {
                required: true,
                message: "请输入菜单名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "上级菜单",
            prop: "parentId",
            type: "tree",
            hide: true,
            props: {
              label: "name",
              value: "id",
            },
            dicUrl: `/system/menu/menuTreeList`,
          },
          {
            label: "标识",
            prop: "code",
            rules: [
              {
                required: true,
                message: "请输入菜单标识",
                trigger: "blur",
              },
            ],
          },
          {
            label: "地址",
            prop: "path",
            align: "left",
            rules: [
              {
                required: true,
                message: "请输入地址",
                trigger: "blur",
              },
            ],
          },
          {
            label: "菜单类型",
            prop: "category",
            type: "radio",
            props: {
              label: "name",
              value: "dictValue",
            },
            dicUrl: `/system/dict/netDict/menu_type`,
            rules: [
              {
                required: true,
                message: "选择菜单类型",
                trigger: "blur",
              },
            ],
          },
          {
            label: "外链",
            prop: "link",
            display: true,
            type: "radio",
            props: {
              label: "name",
              value: "dictValue",
            },
            dicUrl: `/system/dict/netDict/if`,
            rules: [
              {
                required: true,
                message: "选择是否为外链",
                trigger: "blur",
              },
            ],
          },
          {
            label: "菜单图标",
            prop: "source",
            type: "icon",
            iconList: iconList,
          },
          {
            label: "缓存",
            prop: "keepAlive",
            type: "radio",
            props: {
              label: "name",
              value: "dictValue",
            },
            dicUrl: `/system/dict/netDict/if`,
          },
          {
            label: "排序",
            prop: "sort",
            rules: [
              {
                required: true,
                message: "排序",
                trigger: "blur",
              },
            ],
          },


        ],
      },
    };
  },
  watch: {
    'form.category'(val) {
      if (val === 0) {
        this.fromField.link.display = true
        this.fromField.source.display = true
      } else if (val === 1) {
        this.fromField.link.display = false
        this.fromField.source.display = false
      }
    }
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        copyBtn: this.vaildData(this.permission.menu_copy, false),
        viewBtn: this.vaildData(this.permission.menu_view, false),
        addBtn: this.vaildData(this.permission.menu_add, false),
        delBtn: this.vaildData(this.permission.menu_delete, false),
        editBtn: this.vaildData(this.permission.menu_edit, false),
      };
    },
    delIds() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id + "");
      });
      return ids.join(",") + "";
    },
  },
  created() {
    console.log(this.$route)
  },
  methods: {
    exports() {
      let url = exports()
      const a = document.createElement('a')
      a.href = url
      a.click();
    },
    createBtn() {
      this.fullscreenLoading = true;
      createBtn(this.selectionList).then(() => {
        this.fullscreenLoading = false;
        this.onLoad(this.page);
      });
    },
    onLoad(page, param) {
      this.loading = true;
      menuTreeList(page.current, page.pageSize, param).then((res) => {
        this.loading = false;
        this.data = res.data.data;
      });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        query(this.form.id).then((res) => {
          console.log(res);
          this.form = res.data.data;
        });
      }
      done();
    },
    save(row, loading, done) {
      loading();
      save(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!",
        });
      });
      done();
    },
    deletaAll() {
      if (this.selectionList.length <= 0) {
        this.$message.error("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            console.log(this.delIds)
            return del(this.delIds);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          });
    },
    del(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            return del(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
            });
          });
    },
    update(row, index, loading, done) {
      loading();
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!",
        });
      });
      done();
    },
    currentChange() {
    },
    refresh() {
      this.onLoad(this.page, "");
    },
    sizeChange() {
    },
    search(params, done) {
      this.onLoad(this.page, params);
      console.log(params);
      done();
    },
    selectChange(list) {
      this.selectionList = list;
    },
  },
};
</script>
